package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.QyiGoodsSupplier;
import com.ruoyi.system.mapper.QyiGoodsSupplierMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.QyiGoodsMapper;
import com.ruoyi.system.domain.QyiGoods;
import com.ruoyi.system.service.IQyiGoodsService;
import com.ruoyi.common.core.text.Convert;

/**
 * 商品管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-20
 */
@Service
public class QyiGoodsServiceImpl implements IQyiGoodsService 
{
    @Autowired
    private QyiGoodsMapper qyiGoodsMapper;

    @Autowired
    private QyiGoodsSupplierMapper qyiGoodsSupplierMapper;

    /**
     * 查询商品管理
     * 
     * @param goodsId 商品管理主键
     * @return 商品管理
     */
    @Override
    public QyiGoods selectQyiGoodsByGoodsId(Long goodsId)
    {
        return qyiGoodsMapper.selectQyiGoodsByGoodsId(goodsId);
    }

    /**
     * 查询商品管理列表
     * 
     * @param qyiGoods 商品管理
     * @return 商品管理
     */
    @Override
    public List<QyiGoods> selectQyiGoodsList(QyiGoods qyiGoods)
    {
        List<QyiGoods> qyiGoodsList =  qyiGoodsMapper.selectQyiGoodsList(qyiGoods);
        List<QyiGoods> newGoodsList =  new ArrayList<QyiGoods>();

        for (QyiGoods goods : qyiGoodsList){
            goods.setSupplierName(goods.getSupplier().getSupplierName());
            //newGoodsList.add(goods);
        }
        return qyiGoodsList;
    }

    /**
     * 新增商品管理
     * 
     * @param qyiGoods 商品管理
     * @return 结果
     */
    @Override
    public int insertQyiGoods(QyiGoods qyiGoods)
    {
        qyiGoods.setCreateTime(DateUtils.getNowDate());
        int row = qyiGoodsMapper.insertQyiGoods(qyiGoods);
        insertGoodsSupplier(qyiGoods);
        return row;

    }

    private void insertGoodsSupplier(QyiGoods qyiGoods) {
        Long supplierId = qyiGoods.getSupplierId();
        Long goodsId = qyiGoods.getGoodsId();
        if (StringUtils.isNotNull(goodsId) && StringUtils.isNotNull(supplierId)){
            QyiGoodsSupplier goodsSupplier = new QyiGoodsSupplier();
            goodsSupplier.setGoodsId(goodsId);
            goodsSupplier.setSupplierId(supplierId);
            List<QyiGoodsSupplier> list = new ArrayList<>();
            list.add(goodsSupplier);
            if (list.size() > 0){
                qyiGoodsSupplierMapper.batchGoodsSupplier(list);
            }
        }
    }

    /**
     * 修改商品管理
     * 
     * @param qyiGoods 商品管理
     * @return 结果
     */
    @Override
    public int updateQyiGoods(QyiGoods qyiGoods)
    {
        qyiGoods.setUpdateTime(DateUtils.getNowDate());
        int row = qyiGoodsMapper.updateQyiGoods(qyiGoods);

        updateGoodsSupplier(qyiGoods);
        return row;
    }

    private void updateGoodsSupplier(QyiGoods qyiGoods) {

        Long supplierId =qyiGoods.getSupplierId();
        Long goodsId = qyiGoods.getGoodsId();
        // 删除当前商品所以得供应商
        qyiGoodsSupplierMapper.deleteGoodsSupplierByGoodsId(goodsId);
        // 添加当前得新的供应商
        insertGoodsSupplier(qyiGoods);
    }

    /**
     * 批量删除商品管理
     * 
     * @param ids 需要删除的商品管理主键
     * @return 结果
     */
    @Override
    public int deleteQyiGoodsByGoodsIds(String ids)
    {
        Long[] goodsId = Convert.toLongArray(ids);

        qyiGoodsSupplierMapper.deleteGoodsSupplier(goodsId);

        return qyiGoodsMapper.deleteQyiGoodsByGoodsIds(goodsId);
    }

    /**
     * 删除商品管理信息
     * 
     * @param goodsId 商品管理主键
     * @return 结果
     */
    @Override
    public int deleteQyiGoodsByGoodsId(Long goodsId)
    {
        // 刪除商品供貨商關聯
        qyiGoodsSupplierMapper.deleteGoodsSupplierByGoodsId(goodsId);

        return qyiGoodsMapper.deleteQyiGoodsByGoodsId(goodsId);
    }
}
